package com.one.consumer.user.presenter;

import android.content.Context;

import com.one.common.common.user.modle.UserInfoModel;
import com.one.common.config.CMemoryData;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.ImageFactory;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.consumer.user.ui.SetNickNameActivity;
import com.one.consumer.user.ui.view.SetUserView;
import com.one.consumer.user.ui.view.UserView;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import static com.one.common.model.http.NetConstant.IMG_HOST;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/11/6 4:40 PM
 * description ：
 **/
public class UserPresenter extends BaseApiPresenter<SetUserView, UserInfoModel> {
    public UserPresenter(SetUserView mView, Context mContext) {
        super(mView, mContext, new UserInfoModel((BaseActivity) mContext));
    }

    public void getUserInfo() {
        mModel.getUserInfo(new ObserverOnResultListener<UserInfoBean>() {
            @Override
            public void onResult(UserInfoBean userInfoBean) {
                if (userInfoBean != null) {
                    CMemoryData.setUserInfo(userInfoBean);
                    mView.setUserInfo(userInfoBean);
                }
            }
        });
    }

    /**
     * 提交
     */
    private UserInfoBean userInfoBean;
    public void submit(String imgPath, String name) {
        userInfoBean = CMemoryData.getUserInfo();
        if (StringUtils.isNotBlank(imgPath)) {
            uploadImg(new File(imgPath));
        }
        if (StringUtils.isNotBlank(name)) {
            if(name.length()>10){
                Toaster.showLongToast("用户昵称最长10个字符", MyToast.FAIL);
                return;
            }
            userInfoBean.setMember_nick_name(name);
            params.put("member_nick_name", name);
            updateUserInfo();
        }
    }

    private Map<String, String> params = new HashMap<>();

    /**
     * 修改用户信息，头像和姓名
     */
    private void updateUserInfo() {

        new UserInfoModel(mActivity).updateUserInfo(params, s -> {
            if (mView != null) {
                if (mActivity instanceof SetNickNameActivity) {
                    CMemoryData.setUserInfo(userInfoBean);
                    mView.finishPage();
                }
                Toaster.showLongToast("修改成功", MyToast.SUCCESS);
            }
        });
    }


    /**
     * 上传图片
     */
    public void uploadImg(File file) {
        if (file.exists()) {
            new UserInfoModel(mActivity).uploadImg(getImageFile(file.getPath()), new ObserverOnResultListener<String>() {
                @Override
                public void onResult(String s) {
                    String url = IMG_HOST + s;
                    mView.setAvatar(url);
                    params.put("member_img", s);
                    userInfoBean.setMember_img(s);
                    updateUserInfo();
                }
           });
        }

    }

    /**
     * 缩放图片
     */
    private File getImageFile(String path) {
        String outFilePath = mContext.getExternalCacheDir().getPath() + System.currentTimeMillis() + ".jpg";
        ImageFactory imageFactory = new ImageFactory();
        try {
            imageFactory.ratio(imageFactory.getBitmap(path), 480, 480, outFilePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new File(outFilePath);
    }
}
